<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateAdminOperationLogsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('admin_operation_logs', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('admin_user_id'); // A端用户ID
            $table->string('action'); // 操作动作
            $table->string('module'); // 操作模块
            $table->string('target_type')->nullable(); // 操作对象类型
            $table->unsignedBigInteger('target_id')->nullable(); // 操作对象ID
            $table->text('description'); // 操作描述
            $table->json('request_data')->nullable(); // 请求数据
            $table->json('response_data')->nullable(); // 响应数据
            $table->string('ip_address'); // IP地址
            $table->string('user_agent')->nullable(); // 用户代理
            $table->enum('result', ['success', 'failed']); // 操作结果
            $table->timestamps();
            
            // 外键和索引
            $table->foreign('admin_user_id')->references('id')->on('admin_users')->onDelete('cascade');
            $table->index(['admin_user_id', 'created_at']);
            $table->index(['module', 'action']);
            $table->index(['target_type', 'target_id']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('admin_operation_logs');
    }
}